package com.example.idachuappone.person.item;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;

import com.example.idachuappone.BaseActivity;
import com.example.idachuappone.R;
import com.example.idachuappone.cook.action.CookAction;
import com.example.idachuappone.cook.adapter.CookTwoAdapter;
import com.example.idachuappone.cook.entity.Cook;
import com.example.idachuappone.cook.item.CookDetailActivity;
import com.example.idachuappone.utils.AppShareData;
import com.example.idachuappone.utils.BitmapUtilHelper;
import com.example.idachuappone.utils.CheckUtil;
import com.example.idachuappone.utils.ComUtil;
import com.example.idachuappone.utils.Constant;
import com.example.idachuappone.utils.MainToast;
import com.example.idachuappone.utils.NetUtil;
import com.example.idachuappone.utils.PrefUtil;
import com.lidroid.xutils.bitmap.BitmapGlobalConfig.BitmapCacheManagementTask;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;

public class CollectActivity extends BaseActivity implements OnItemClickListener, OnClickListener {
	private Cook cook;
	private CookAction cookAction;
	private CookTwoAdapter cookTwoAdapter;
	private LinearLayout ll_no_network;
	private LinearLayout ll_none;
	private ListView lv_cook;
	private Dialog mCusProDialog;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_collect);
		this.cook = new Cook();
		this.cookAction = new CookAction();
		this.mCusProDialog = ComUtil.createCusProgressSmallBlackBgDialog(this);
		initView();
		checkNet();
	}

	private void initView() {
		this.ll_no_network = (LinearLayout) findViewById(R.id.ll_no_network);
		this.ll_no_network.setOnClickListener(this);
		this.ll_none = (LinearLayout) findViewById(R.id.ll_none);
		this.btn_back = (Button) findViewById(R.id.btn_back);
		this.btn_back.setOnClickListener(this);
		this.lv_cook = (ListView) findViewById(R.id.lv_cook);
		this.cookTwoAdapter = new CookTwoAdapter(this, BitmapUtilHelper.createBitmapUtil(this));
		this.cookTwoAdapter.setLocation(PrefUtil.getInstance(this).getLocateCoor());
		this.lv_cook.setAdapter(this.cookTwoAdapter);
		this.lv_cook.setOnItemClickListener(this);
	}

	private void initCookAdapter() {
		if (this.cook.getResults() == null || this.cook.getResults().size() <= 0) {
			this.ll_none.setVisibility(0);
		} else {
			this.ll_none.setVisibility(BitmapCacheManagementTask.MESSAGE_CLEAR_MEMORY_BY_KEY);
		}
		this.cookTwoAdapter.setList(this.cook.getResults());
		this.cookTwoAdapter.notifyDataSetChanged();
	}

	private void checkNet() {
		if (CheckUtil.checkNet(this)) {
			initData();
		} else {
			this.ll_no_network.setVisibility(0);
		}
	}

	private void initData() {
		NetUtil.get(this, Constant.LIST_COLLECT, 100, new RequestCallBack<String>() {
			@Override
			public void onStart() {
				super.onStart();
				CollectActivity.this.mCusProDialog.show();
			}

			@Override
			public void onSuccess(ResponseInfo<String> responseInfo) {
				CollectActivity.this.mCusProDialog.dismiss();
				CollectActivity.this.ll_no_network.setVisibility(BitmapCacheManagementTask.MESSAGE_CLEAR_MEMORY_BY_KEY);
				try {
					JSONObject jsonObject = new JSONObject(NetUtil.unescapeUnicode(responseInfo.result));
					if (jsonObject.getInt("code") == 10000) {
						CollectActivity.this.cook.parseJson(jsonObject.getJSONObject("data"));
						CollectActivity.this.initCookAdapter();
					} else if (jsonObject.getInt("code") == 300) {
						MainToast.show(CollectActivity.this, jsonObject.getString("error"), 0);
					}
				} catch (JSONException e) {
					e.printStackTrace();
					MainToast.show(CollectActivity.this, CollectActivity.this.getResources()
							.getString(R.string.netFail), 0);
				}
			}

			@Override
			public void onFailure(HttpException arg0, String arg1) {
				CollectActivity.this.mCusProDialog.dismiss();
				CollectActivity.this.ll_no_network.setVisibility(0);
			}
		});
	}

	@Override
	public void onItemClick(AdapterView<?> adapterView, View arg1, int position, long arg3) {
		String result = this.cookAction.isCookService(this.cook.getResults().get(position));
		if (result == null) {
			Intent intent = new Intent(this, CookDetailActivity.class);
			Bundle bundle = new Bundle();
			bundle.putString("id", this.cook.getResults().get(position).getId());
			bundle.putInt("cookDetailid", position);
			bundle.putString("location", PrefUtil.getInstance(this).getLocateCoor());
			intent.putExtras(bundle);
			startActivityForResult(intent, 1);
			return;
		}
		MainToast.show(this, result, 0);
	}

	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if (requestCode == 1 && resultCode == -1 && PrefUtil.getInstance(this).getUID() != null
				&& PrefUtil.getInstance(this).getUID().trim().length() > 0) {
			checkNet();
		}
	}

	@Override
	protected void onResume() {
		super.onResume();
		if (AppShareData.isRefreshCollect) {
			initData();
			AppShareData.isRefreshCollect = false;
		}
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_back /* 2131165198 */:
			finish();
		case R.id.ll_no_network /* 2131165232 */:
			checkNet();
		default:
		}
	}
}
